package com.aaa.service.impl;

import com.aaa.dao.RecharGeruleDao;
import com.aaa.dao.impl.RecharGeruleDaoImpl;
import com.aaa.entity.RecharGerule;
import com.aaa.service.RecharGeruleService;
import com.aaa.util.BusinessException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RecharGeruleServiceImpl implements RecharGeruleService {
    private RecharGeruleDao recharGeruleDao = new RecharGeruleDaoImpl();

    @Override
    public int addRecharGerule(RecharGerule recharGerule) {
        return recharGeruleDao.addRecharGerule(recharGerule);
    }

    @Override
    public int updateRecharGeruleById(RecharGerule recharGerule) {
        return recharGeruleDao.updateRecharGeruleById(recharGerule);
    }

    @Override
    public int updateRecharGeruleStatusById(Integer id, Integer status) {
        if (status == 0){
            status = 1;
        }else {
            status = 0;
        }

        return recharGeruleDao.updateRecharGeruleStatusById(id,status);
    }

    @Override
    public Map<String,Object> findAllAndSearchRecharGerule(Integer pageNumber, Integer pageSize, String name, String status) {
        int count = findAllAndSearchRecharGeruleCount(name,status);
        try {
            if (pageNumber == null || pageNumber == 0) {
                throw new BusinessException("当前页数不能为空");
            }
            if (pageSize == null || pageSize == 0) {
                throw new BusinessException("每页条数不能为空");
            }
            pageNumber = (pageNumber - 1) * pageSize;
        } catch (Exception e){
            e.printStackTrace();
        }
        List<RecharGerule> list = recharGeruleDao.findAllAndSearchRecharGerule(pageNumber,pageSize,name,status);
        Map<String,Object> map = new HashMap();
        map.put("count",count);
        map.put("list",list);
        return map;
    }

    @Override
    public int findAllAndSearchRecharGeruleCount(String name, String status) {
        return recharGeruleDao.findAllAndSearchRecharGeruleCount(name,status);
    }
    @Override
    public List<RecharGerule> getRechargeruleList() {
        return recharGeruleDao.getRechargeruleList();
    }
}
